{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Text_Summarization.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sFYgfvJKjNkS",
        "outputId": "937b072b-6664-45e9-eaf7-98d2e9fbf9b1"
      },
      "source": [
        "!pip install torch\n",
        "!pip install git+https://github.com/huggingface/transformers.git"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (1.7.0+cu101)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch) (3.7.4.3)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch) (1.18.5)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch) (0.16.0)\n",
            "Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from torch) (0.8)\n",
            "Collecting git+https://github.com/huggingface/transformers.git\n",
            "  Cloning https://github.com/huggingface/transformers.git to /tmp/pip-req-build-duszsuzn\n",
            "  Running command git clone -q https://github.com/huggingface/transformers.git /tmp/pip-req-build-duszsuzn\n",
            "  Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
            "  Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
            "    Preparing wheel metadata ... \u001b[?25l\u001b[?25hdone\n",
            "Requirement already satisfied (use --upgrade to upgrade): transformers==4.1.0.dev0 from git+https://github.com/huggingface/transformers.git in /usr/local/lib/python3.6/dist-packages\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (2.23.0)\n",
            "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (2019.12.20)\n",
            "Requirement already satisfied: tokenizers==0.9.4 in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (0.9.4)\n",
            "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (4.41.1)\n",
            "Requirement already satisfied: dataclasses; python_version < \"3.7\" in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (0.8)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (1.18.5)\n",
            "Requirement already satisfied: sacremoses in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (0.0.43)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (3.0.12)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.6/dist-packages (from transformers==4.1.0.dev0) (20.4)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->transformers==4.1.0.dev0) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->transformers==4.1.0.dev0) (2020.11.8)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->transformers==4.1.0.dev0) (1.24.3)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->transformers==4.1.0.dev0) (3.0.4)\n",
            "Requirement already satisfied: click in /usr/local/lib/python3.6/dist-packages (from sacremoses->transformers==4.1.0.dev0) (7.1.2)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from sacremoses->transformers==4.1.0.dev0) (1.15.0)\n",
            "Requirement already satisfied: joblib in /usr/local/lib/python3.6/dist-packages (from sacremoses->transformers==4.1.0.dev0) (0.17.0)\n",
            "Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.6/dist-packages (from packaging->transformers==4.1.0.dev0) (2.4.7)\n",
            "Building wheels for collected packages: transformers\n",
            "  Building wheel for transformers (PEP 517) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for transformers: filename=transformers-4.1.0.dev0-cp36-none-any.whl size=1375102 sha256=726766ced8590ac36c4cc917f6212e73795a3f4a0b2fb501b05bd846f29a38b0\n",
            "  Stored in directory: /tmp/pip-ephem-wheel-cache-3lxcjkz9/wheels/33/eb/3b/4bf5dd835e865e472d4fc0754f35ac0edb08fe852e8f21655f\n",
            "Successfully built transformers\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OhgcbS5Ugf_G",
        "outputId": "0a4829eb-ed20-4406-aebd-e7c32e751b42"
      },
      "source": [
        "from transformers import pipeline\n",
        "\n",
        "# using pipeline API for summarization task\n",
        "summarization = pipeline(\"summarization\")\n",
        "original_text = \"\"\"\n",
        "Paul Walker is hardly the first actor to die during a production. \n",
        "But Walker's death in November 2013 at the age of 40 after a car crash was especially eerie given his rise to fame in the \"Fast and Furious\" film franchise. \n",
        "The release of \"Furious 7\" on Friday offers the opportunity for fans to remember -- and possibly grieve again -- the man that so many have praised as one of the nicest guys in Hollywood. \n",
        "\"He was a person of humility, integrity, and compassion,\" military veteran Kyle Upham said in an email to CNN. \n",
        "Walker secretly paid for the engagement ring Upham shopped for with his bride. \n",
        "\"We didn't know him personally but this was apparent in the short time we spent with him. \n",
        "I know that we will never forget him and he will always be someone very special to us,\" said Upham. \n",
        "The actor was on break from filming \"Furious 7\" at the time of the fiery accident, which also claimed the life of the car's driver, Roger Rodas. \n",
        "Producers said early on that they would not kill off Walker's character, Brian O'Connor, a former cop turned road racer. Instead, the script was rewritten and special effects were used to finish scenes, with Walker's brothers, Cody and Caleb, serving as body doubles. \n",
        "There are scenes that will resonate with the audience -- including the ending, in which the filmmakers figured out a touching way to pay tribute to Walker while \"retiring\" his character. At the premiere Wednesday night in Hollywood, Walker's co-star and close friend Vin Diesel gave a tearful speech before the screening, saying \"This movie is more than a movie.\" \"You'll feel it when you see it,\" Diesel said. \"There's something emotional that happens to you, where you walk out of this movie and you appreciate everyone you love because you just never know when the last day is you're gonna see them.\" There have been multiple tributes to Walker leading up to the release. Diesel revealed in an interview with the \"Today\" show that he had named his newborn daughter after Walker. \n",
        "Social media has also been paying homage to the late actor. A week after Walker's death, about 5,000 people attended an outdoor memorial to him in Los Angeles. Most had never met him. Marcus Coleman told CNN he spent almost $1,000 to truck in a banner from Bakersfield for people to sign at the memorial. \"It's like losing a friend or a really close family member ... even though he is an actor and we never really met face to face,\" Coleman said. \"Sitting there, bringing his movies into your house or watching on TV, it's like getting to know somebody. It really, really hurts.\" Walker's younger brother Cody told People magazine that he was initially nervous about how \"Furious 7\" would turn out, but he is happy with the film. \"It's bittersweet, but I think Paul would be proud,\" he said. CNN's Paul Vercammen contributed to this report.\n",
        "\"\"\"\n",
        "summary_text = summarization(original_text)[0]['summary_text']\n",
        "print(\"Summary:\", summary_text)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Summary:  Paul Walker died in November 2013 after a car crash in Los Angeles . The late actor was one of the nicest guys in Hollywood . The release of \"Furious 7\" on Friday offers a chance to grieve again . There have been multiple tributes to Walker leading up to the film's release .\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "esw2Bi0ajCIF",
        "outputId": "305a2b69-1468-4b59-df3c-35c8c3925717"
      },
      "source": [
        "original_text =\"\"\"\n",
        "The US has \"passed the peak\" on new coronavirus cases, President Donald Trump said and predicted that some states would reopen this month.\n",
        "The US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths, the highest for any country in the world.\n",
        "At the daily White House coronavirus briefing on Wednesday, Trump said new guidelines to reopen the country would be announced on Thursday after he speaks to governors.\n",
        "\"We'll be the comeback kids, all of us,\" he said. \"We want to get our country back.\"\n",
        "The Trump administration has previously fixed May 1 as a possible date to reopen the world's largest economy, but the president said some states may be able to return to normalcy earlier than that.\n",
        "\"\"\"\n",
        "summary_text = summarization(original_text,min_length=5, max_length=150)[0]['summary_text']\n",
        "print(\"Summary:\", summary_text)"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Summary:  The US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths, the highest for any country in the world . New guidelines to reopen the country will be announced on Thursday after he speaks to governors .\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "L-m8JGJlkCaI"
      },
      "source": [
        "# Using T5ForConditionalGeneration, T5Tokenizer"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FK5wJEDfjtmt",
        "outputId": "96e7dee7-9bea-478e-db68-a3265db32fab"
      },
      "source": [
        "from transformers import T5ForConditionalGeneration, T5Tokenizer\n",
        "\n",
        "# initialize the model architecture and weights\n",
        "model = T5ForConditionalGeneration.from_pretrained(\"t5-base\")\n",
        "# initialize the model tokenizer\n",
        "tokenizer = T5Tokenizer.from_pretrained(\"t5-base\")"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Some weights of the model checkpoint at t5-base were not used when initializing T5ForConditionalGeneration: ['decoder.block.0.layer.1.EncDecAttention.relative_attention_bias.weight']\n",
            "- This IS expected if you are initializing T5ForConditionalGeneration from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
            "- This IS NOT expected if you are initializing T5ForConditionalGeneration from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "taqIM_yEkKTZ",
        "outputId": "752b6a96-1571-408e-aa28-b1b2f12da215"
      },
      "source": [
        "article = \"\"\"\n",
        "Justin Timberlake and Jessica Biel, welcome to parenthood. \n",
        "The celebrity couple announced the arrival of their son, Silas Randall Timberlake, in statements to People. \n",
        "\"Silas was the middle name of Timberlake's maternal grandfather Bill Bomar, who died in 2012, while Randall is the musician's own middle name, as well as his father's first,\" People reports. \n",
        "The couple announced the pregnancy in January, with an Instagram post. It is the first baby for both.\n",
        "\"\"\"\n",
        "# encode the text into tensor of integers using the appropriate tokenizer\n",
        "inputs = tokenizer.encode(\"summarize: \" + article, return_tensors=\"pt\", max_length=512, truncation=True)\n",
        "# generate the summarization output\n",
        "outputs = model.generate(\n",
        "    inputs, \n",
        "    max_length=150, \n",
        "    min_length=40, \n",
        "    length_penalty=2.0, \n",
        "    num_beams=4, \n",
        "    early_stopping=True)\n",
        "# just for debugging\n",
        "print(outputs)\n",
        "print(tokenizer.decode(outputs[0]))"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[    0,     8,  1158,  2162,     8,  8999,    16,  1762,     3,     5,\n",
            "            34,    19,     8,   166,  1871,    21,   321,    13,   135,     3,\n",
            "             5,     8,  1871,  4940,    19,     8,  2214,   564,    13, 12446,\n",
            "         25045, 16948,    31,     7, 28574, 18573,     6,   113,  3977,    16,\n",
            "          1673,     3,     5,     1]])\n",
            "<pad> the couple announced the pregnancy in January. it is the first baby for both of them. the baby boy is the middle name of Justin Timberlake's maternal grandfather, who died in 2012.</s>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fFQREa9ZnLJM",
        "outputId": "9e0bf9d4-519b-41e5-8a06-22b56ceaf589"
      },
      "source": [
        "article = \"\"\"\n",
        "The US has \"passed the peak\" on new coronavirus cases, President Donald Trump said and predicted that some states would reopen this month.\n",
        "The US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths, the highest for any country in the world.\n",
        "At the daily White House coronavirus briefing on Wednesday, Trump said new guidelines to reopen the country would be announced on Thursday after he speaks to governors.\n",
        "\"We'll be the comeback kids, all of us,\" he said. \"We want to get our country back.\"\n",
        "The Trump administration has previously fixed May 1 as a possible date to reopen the world's largest economy, but the president said some states may be able to return to normalcy earlier than that.\n",
        "\"\"\"\n",
        "# encode the text into tensor of integers using the appropriate tokenizer\n",
        "inputs = tokenizer.encode(\"summarize: \" + article, return_tensors=\"pt\", max_length=512, truncation=True)\n",
        "# generate the summarization output\n",
        "outputs = model.generate(\n",
        "    inputs, \n",
        "    max_length=150, \n",
        "    min_length=40, \n",
        "    length_penalty=2.0, \n",
        "    num_beams=4, \n",
        "    early_stopping=True)\n",
        "# just for debugging\n",
        "print(outputs)\n",
        "print(tokenizer.decode(outputs[0]))"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[    0,     8,   837,    65,   147,     3,  3891, 18834,  5899,   638,\n",
            "          6961,  4481,  1488,    11,   147, 11558,   927,  2688, 14319,     3,\n",
            "             5,    96,  1123,    31,   195,    36,     8,   369,  1549,  1082,\n",
            "             6,    66,    13,   178,   976,     3,    88,   845,     3,     5,\n",
            "             8,  2753,  9689,     7,   128,  2315,    56,     3,    60,  8751,\n",
            "            48,   847,     3,     5,     1]])\n",
            "<pad> the US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths. \"we'll be the comeback kids, all of us,\" he says. the president predicts some states will reopen this month.</s>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CHgQ5OXsnsdI"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}